package com.example.chapter08.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.chapter08.R;

public class DynamicFragment extends Fragment {
    private final String TAG = "DynamicFragment";

    public static DynamicFragment newInstance(int position, int image_id, String desc) {
        DynamicFragment fragment = new DynamicFragment();
        // 把参数打包，传入到Fragment中
        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putInt("image_id", image_id);
        args.putString("desc", desc);
        fragment.setArguments(args);
        return fragment;
    }

    private int getPosition() {
        Bundle args = getArguments();
        return args != null ? args.getInt("position") : 0;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.d(TAG, "fragment onAttach position: " + getPosition());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "fragment onCreate position: " + getPosition());
    }

    // 创建Fragment视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 根据布局文件fragment_dynamic.xml生成视图对象
        View view = inflater.inflate(R.layout.fragment_dynamic, container, false);
        Bundle arguments = getArguments();
        if (arguments != null) {
            ImageView iv_pic = view.findViewById(R.id.iv_pic);
            TextView tv_desc = view.findViewById(R.id.tv_desc);
            iv_pic.setImageResource(arguments.getInt("image_id", R.drawable.vivo));
            tv_desc.setText(arguments.getString("desc"));
        }
        Log.d(TAG, "fragment onCreateView position: " + getPosition());
        return view;
    }

    // 页面启动
    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "Fragment onStart position: " + getPosition());
    }

    // 页面恢复
    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "Fragment onResume position: " + getPosition());
    }

    // 页面暂停
    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "Fragment onPause position: " + getPosition());
    }

    // 页面停止
    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "Fragment onStop position: " + getPosition());
    }

    // 销毁Fragment的页面视图
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "Fragment onDestroyView position: " + getPosition());
    }

    // 销毁页面
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Fragment onDestroy position: " + getPosition());
    }

    // 从页面上移除Fragment
    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG, "Fragment onDetach position: " + getPosition());
    }
}